using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms;
using Auxiliaries;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Auxiliary form to add new message ids and statuses
    /// </summary>
    public partial class AEUCAddMSGItem : Form
    {
        /// <summary>
        /// Possible types of message items that can be created
        /// </summary>
        public enum MSGItemType
        {
            /// <summary>
            /// message id item
            /// </summary>
            AEUC_MSG_ID,
            /// <summary>
            /// message status item
            /// </summary>
            AEUC_MSG_STAT,
            /// <summary>
            /// system message id item (not used)
            /// </summary>
            SPECMSG_MSG_ID,
            /// <summary>
            /// system message status item (not used)
            /// </summary>
            SPECMSG_MSG_STAT,
            /// <summary>
            /// message destination type item
            /// </summary>
            MSG_DEST,
            /// <summary>
            /// editted message id item - special case when editting existing items
            /// </summary>
            EDIT_MSG_ID,
            /// <summary>
            /// editted message status item - special case when editting existing items
            /// </summary>
            EDIT_MSG_STAT
        }

        CommonMSGControl cmc;
        MSGItemType itemType;
        GenMSGId editAgmi;
        GenMSGStatus editAgms;
        /// <summary>
        /// Launches an instances of the auxiliary message item creation form
        /// </summary>
        /// <param name="icmc">Reference to the common message structure</param>
        /// <param name="iitemType">Enumeration of possible items: 0 aeuc id; 1 aeuc status; 2 SPECMSG id;3 SPECMSG status;4 target destination</param>
        public AEUCAddMSGItem(CommonMSGControl icmc, MSGItemType iitemType)
        {
            InitializeComponent();
            cmc = icmc;
            itemType = iitemType;
        }

        /// <summary>
        /// Launches an instances of the auxiliary message item creation form for editing
        /// </summary>
        /// <param name="icmc">Reference to the common message structure</param>
        /// <param name="iAgmi">Generic message item to be edited</param>
        public AEUCAddMSGItem(CommonMSGControl icmc, GenMSGId iAgmi)
        {
            InitializeComponent();
            cmc = icmc;
            itemType = MSGItemType.EDIT_MSG_ID;

            tbDisplayString.Text = iAgmi.DisplayString;
            tbUID.Text = iAgmi.UID.ToString();

            editAgmi = iAgmi;
        }

        /// <summary>
        /// Launches an instances of the auxiliary message item creation form for editing
        /// </summary>
        /// <param name="icmc">Reference to the common message structure</param>
        /// <param name="iAgms">Generic message item to be edited</param>
        public AEUCAddMSGItem(CommonMSGControl icmc, GenMSGStatus iAgms)
        {
            InitializeComponent();
            cmc = icmc;
            itemType = MSGItemType.EDIT_MSG_STAT;

            tbDisplayString.Text = iAgms.DisplayString;
            tbUID.Text = iAgms.UID.ToString();

            editAgms = iAgms;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            uint val;
            bool faulty_input = false;
            if (!uint.TryParse(tbUID.Text, out val))
            {
                UInt64 tmpVal;
                faulty_input = !HEX_converter.ToBinary(tbUID.Text, out tmpVal);
                val = (uint)tmpVal;
            }
            if (!faulty_input)
            {
                switch (itemType)
                {
                    case  MSGItemType.AEUC_MSG_ID:
                        GenMSGId agmi = new GenMSGId();
                        agmi.UID = val;
                        agmi.DisplayString = tbDisplayString.Text;
                        cmc.AEUC_MSG_ID.Add(agmi);
                        break;
                    case MSGItemType.AEUC_MSG_STAT:
                        GenMSGStatus agms = new GenMSGStatus();
                        agms.UID = val;
                        agms.DisplayString = tbDisplayString.Text;
                        cmc.AEUC_MSG_STAT.Add(agms);
                        break;
                    case MSGItemType.SPECMSG_MSG_ID:
                        GenMSGId hgmi = new GenMSGId();
                        hgmi.UID = val;
                        hgmi.DisplayString = tbDisplayString.Text;
                        cmc.SPECMSG_MSG_ID.Add(hgmi);
                        break;
                    case MSGItemType.SPECMSG_MSG_STAT:
                        GenMSGStatus hgms = new GenMSGStatus();
                        hgms.UID = val;
                        hgms.DisplayString = tbDisplayString.Text;
                        cmc.SPECMSG_MSG_STAT.Add(hgms);
                        break;
                    case MSGItemType.MSG_DEST:
                        GenMSGDestination gmd = new GenMSGDestination();
                        gmd.UID = val;
                        gmd.DisplayString = tbDisplayString.Text;
                        cmc.MSG_DEST.Add(gmd);
                        break;
                    case MSGItemType.EDIT_MSG_ID:
                        editAgmi.UID = val;
                        editAgmi.DisplayString = tbDisplayString.Text;
                        break;
                    case MSGItemType.EDIT_MSG_STAT:
                        editAgms.UID = val;
                        editAgms.DisplayString = tbDisplayString.Text;
                        break;
                }
                Close();
            }
            else
                MessageBox.Show("Invalid UID value.");
        }
    }
}